package com.appspot.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.HTTPRequest;
import com.google.gwt.user.client.ResponseTextHandler;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class vse implements EntryPoint {

  /**
   * This is the entry point method.
   */
  public void onModuleLoad() {
	  CheckLoggedInAndRespond();
	  
	  fillHeadlineDate();
  }
  private void CheckLoggedInAndRespond(){
	//let's do a call to /service/user?action=isloggedin
	HTTPRequest.asyncGet("/service/user?action=isloggedin", new ResponseTextHandler(){
			
	        public void onCompletion(String responseText){
	          if(responseText=="true"){
	        	  showLogoutLink();
	          }
	          else if (responseText=="false"){
	        	  showLoginLink();
	          }
	          else{
	        	//maybe something bad happened  
	        	  RootPanel.get().add(new Label ("Error occured.")); 
	          }
	        }
	});
  }
  
  private void showLoginLink(){
	  HTTPRequest.asyncGet("/service/user?action=getloginhtml", new ResponseTextHandler(){
		  public void onCompletion(String responseText){
			  RootPanel.get("loginlogout-content").add(new HTML(responseText));
		  }
	  });
  }
  
  private void showLogoutLink(){
	  HTTPRequest.asyncGet("/service/user?action=getlogouthtml", new ResponseTextHandler(){
		  public void onCompletion(String responseText){
			  RootPanel.get("loginlogout-content").add(new HTML(responseText));
		  }
	  });
	  
  }
  
  private void fillHeadlineDate(){
	  HTTPRequest.asyncGet("/service/datetime?action=getheadlinedate", new ResponseTextHandler(){
		  public void onCompletion(String responseText){
			  RootPanel.get("headline-date").add(new Label(responseText));
		  }
	  });	  
  }
}
